import Vue from 'vue'
import Vuex from 'vuex'
import {getUserinfo,getConfig} from '@/common/api_user.js'
import appInfo from '@/common/appInfo.js'
import utils from '@/common/utils';
Vue.use(Vuex) // vue的插件机制
let menuButtonInfo = {}
// #ifdef MP-WEIXIN
menuButtonInfo = uni.getMenuButtonBoundingClientRect()
// #endif
// Vuex.Store 构造器选项
const store = new Vuex.Store({
    // 为了不和页面或组件的data中的造成混淆，state中的变量前面建议加上$符号
    state: {
        hasLogin:false,
		userInfo:{},
		currentUserInfo:{}, // 遇见黄石用户信息
		banList:[],
		config:{},
		api_host:appInfo.api_host,
		menuWidth:menuButtonInfo.width || 0
    },
	getters: {
		hasLogin: state => state.hasLogin,
		userInfo: state => state.userInfo,
		currentUserInfo: state => state.currentUserInfo,
		banList: state => state.banList,
		api_host: state => state.api_host,
		config: state => state.config,
		menuWidth: state => state.menuWidth
	},
	mutations: {
		logOut(state,provider){
			state.userInfo = {}
			state.hasLogin = false
			uni.removeStorageSync('token')
			uni.removeStorageSync('userInfo')
		},
		setUserinfo(state,provider){
			state.userInfo = provider
			if(state.userInfo && state.userInfo.userPhone){
				state.hasLogin = true
				console.log(state, 'res.data state')
			}
			uni.setStorageSync('userInfo',state.userInfo)
		},
		setBanlist(state,provider){
			state.banList = provider
		},
		setConfig(state,provider){
			state.config = provider
			console.log(state,'state');
		},
		setCurrentUserInfo(state,provider){
			state.currentUserInfo = provider
			// 可选：持久化存储到本地
			uni.setStorageSync('currentUserInfo', state.currentUserInfo)
		}
	},
	actions: {
		async getUserinfo({commit},payload){
			const res = await getUserinfo()
			if(res.code == 200){
				console.log(res.data, 'res.data')
				commit('setUserinfo',res.data)
			}
		},
		async getConfig({commit},payload){
			const res = await getConfig()
			if(res.code == 200){
				commit('setConfig',res.data)
			}
		}
	}
})

export default store
